Comfy-table
Comfy-table is designed as a library for building beautiful tables, while being easy to use.
Features
- Dynamic arrangement of content depending on a given width.
- ANSI content styling for terminals (Colors, Bold, Blinking, etc.).
- Styling Presets and preset modifiers to get you started.
- Pretty much every part of the table is customizable (borders, lines, padding, alignment).
- Constraints on columns that allow some additional control over how to arrange content.
- Cross plattform (Linux, macOS, Windows).
- It's FAST!
Benchmarks show that a pretty big table with weird constraints is build in 4 μs, which is
4*1e^6
. To run the benchmarks install criterion viacargo install cargo-criterion
and runcargo criterion
afterwards.
Comfy-table is written for the current stable
Rust version.
Older Rust versions may work, but aren't officially supported.
Examples
use Table;
Create a very basic table.
This table will become as wide as your content, nothing fancy happening here.
+----------------------+----------------------+------------------------+
| Header1 | Header2 | Header3 |
+======================================================================+
| This is a text | This is another text | This is the third text |
|----------------------+----------------------+------------------------|
| This is another text | Now | This is awesome |
| | add some | |
| | multi line stuff | |
+----------------------+----------------------+------------------------+
More Features
use *;
use UTF8_FULL;
use UTF8_ROUND_CORNERS;
Create a table with UTF8 styling, and apply a modifier that gives the table round corners.
Additionally, the content will dynamically wrap to maintain a given table width.
If the table width isn't explicitely set and the program runs in a terminal, the terminal size will be used.
On top of this, we set the default alignment for the right column to Right
and the alignment of the left top cell to Center
.
╭────────────┬────────────┬────────────╮
│ Header1 ┆ Header2 ┆ Header3 │
╞════════════╪════════════╪════════════╡
│ This is a ┆ This is ┆ This is │
│ text ┆ another ┆ the third │
│ ┆ text ┆ text │
├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ This is ┆ Now ┆ This is │
│ another ┆ add some ┆ awesome │
│ text ┆ multi line ┆ │
│ ┆ stuff ┆ │
╰────────────┴────────────┴────────────╯
Styling
use *;
use UTF8_FULL;
This code generates the table that can be seen at the top of this document.
Code Examples
A few examples can be found in the example
folder.
To test an example, run it with cargo run --example $name
. E.g.:
If you're looking for more information, take a look at the tests folder.
There are tests for almost every feature including a visual view for each resulting table.
Contribution Guidelines
Comfy-table is supposed to be minimalistic. A fixed set of features that just work, for a simple use-case:
- Normal tables (columns, rows, one cell per column/row).
- Dynamic arrangement of content to a given width.
- Some kind of manual intervention in the arrangement process.
If you come up with an idea or an improvement, that fits into the current scope of the project, feel free to create an issue :)!
Some things however will most likely not be added to the project, since they drastically increase the complexity of the library or cover very specific edge-cases.
Such features are:
- Nested tables
- Cells that span over multiple columns/rows
- CSV to table conversion and vice versa
Unsafe
Comfy-table doesn't allow unsafe
code in its code-base.
As it's a "simple" formatting library, it also shouldn't be needed in the future.
However, Comfy-table uses two unsafe functions calls in its dependencies.
Both calls can be disabled by explicitely calling Table::force_no_tty.
-
crossterm::tty::IsTty
. This function is necessary to detect whether we're currently on a tty or not. This is only called if no explicit width is provided viaTable::set_table_width
./// On unix, the `isatty()` function returns true if a file /// descriptor is a terminal.
-
crossterm::terminal::size
. This function is necessary to detect the current terminal width, if we're on a tty. This is only called if no explicit width is provided viaTable::set_table_width
.http://rosettacode.org/wiki/Terminal_control/Dimensions#Library:_BSD_libc This is another libc call with, which is used to communicate with
/dev/tty
via a file descriptor.... if wrap_with_result.is_ok else ...